package com.neu.questionnaireplus.utils;


import cn.hutool.crypto.digest.MD5;
import com.neu.questionnaireplus.entity.Answerer;
import com.neu.questionnaireplus.utils.common.CommonUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

public class FileToAnswerers {

  public static List<Answerer> importExcel(MultipartFile excelFile) {
    if (excelFile == null || excelFile.getSize() == 0) {
      throw new RuntimeException("文件上传错误，重新上传");
    }
    String filename = excelFile.getOriginalFilename();
    if (!(filename.endsWith(".xls") || filename.endsWith(".xlsx"))) {
      throw new RuntimeException("文件上传格式错误，请重新上传");
    }

    List<Answerer> list = null;
    try {
      if (filename.endsWith(".xls")) {
        list = readXLS(excelFile);
      } else {
        list = readXLSX(excelFile);
      }
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException("文件内容读取失败，请重试");
    }
    return list;
  }

  public static List<Answerer> readXLS( MultipartFile file) throws IOException {
    List<Answerer> list = new ArrayList<>();

    InputStream inputStream = file.getInputStream();
    HSSFWorkbook workbook = new HSSFWorkbook(inputStream);

    //读取第一张sheet
    HSSFSheet sheet = workbook.getSheetAt(0);
    String errorMsg = "";
    //遍历每一行Excel获取内容
    for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
      HSSFRow row = sheet.getRow(rowNum);
      if (row != null) {
        Answerer answerer = new Answerer();
        //使用了getStringCellValue()方法来获取值，POI会判断单元格的类型，如果非字符串类型就会抛出上面的异常。
        //所以先使用setCellType()方法先将该单元格的类型设置为STRING
        //然后poi会根据字符串读取它
        row.getCell(0).setCellType(CellType.STRING);
        row.getCell(1).setCellType(CellType.STRING);
        row.getCell(2).setCellType(CellType.STRING);
        row.getCell(3).setCellType(CellType.STRING);
        row.getCell(4).setCellType(CellType.STRING);
//        链式赋值
        answerer.setId(CommonUtils.getUUID())
            .setUsername(row.getCell(0).getStringCellValue())
            .setPassword(MD5.create().digestHex(row.getCell(1).getStringCellValue()))
            .setRole(0)
            .setCreatedDate(CommonUtils.getDateByDateString(CommonUtils.getFormatDateTimeNow()))
            .setDelFlag(0)
            .setStatus(0)
            .setTelnumber(row.getCell(2).getStringCellValue())
            .setSecurityQuestionContext(row.getCell(3).getStringCellValue())
            .setSecurityQuestionAnswer(row.getCell(4).getStringCellValue());
        list.add(answerer);
      }
    }
//    for (User user : list){
//      System.out.println(user.toString());
//    }
    return list;
  }

  public static List<Answerer> readXLSX(MultipartFile file) throws IOException {
    ArrayList<Answerer> list = new ArrayList<>();

    InputStream inputStream = file.getInputStream();
    XSSFWorkbook Workbook = new XSSFWorkbook(inputStream);

    XSSFSheet sheet = Workbook.getSheetAt(0);
    int lastRowNum = sheet.getLastRowNum();
    for (int rowNum = 1; rowNum <= lastRowNum; rowNum++) {
      XSSFRow row = sheet.getRow(rowNum);
      if (row != null) {
        Answerer answerer = new Answerer();
        //使用了getStringCellValue()方法来获取值，POI会判断单元格的类型，如果非字符串类型就会抛出上面的异常。
        //所以先使用setCellType()方法先将该单元格的类型设置为STRING
        //然后poi会根据字符串读取它
        row.getCell(0).setCellType(CellType.STRING);
        row.getCell(1).setCellType(CellType.STRING);
        row.getCell(2).setCellType(CellType.STRING);
        row.getCell(3).setCellType(CellType.STRING);
        row.getCell(4).setCellType(CellType.STRING);
//        链式赋值
        answerer.setId(CommonUtils.getUUID())
            .setUsername(row.getCell(0).getStringCellValue())
            .setPassword(MD5.create().digestHex(row.getCell(1).getStringCellValue()))
            .setRole(0)
            .setCreatedDate(CommonUtils.getDateByDateString(CommonUtils.getFormatDateTimeNow()))
            .setDelFlag(0)
            .setStatus(0)
            .setTelnumber(row.getCell(2).getStringCellValue())
            .setSecurityQuestionContext(row.getCell(3).getStringCellValue())
            .setSecurityQuestionAnswer(row.getCell(4).getStringCellValue());
        list.add(answerer);
      }
    }

//    for (User user : list) {
//      System.out.println(user.toString());
//    }
    return list;
  }
}
